package com.link510.aitools.framework.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.link510.aitools.R;
import com.link510.aitools.framework.view.audio.AudioPlayManager;
import com.link510.aitools.framework.view.audio.IAudioPlayListener;
import com.link510.aitools.framework.view.audio.widget.VoiceImageView;

import java.io.File;
import java.util.List;

/**
 * 适配器
 */
public abstract class AudioShoutingAdapter extends BaseAdapter {

    private LayoutInflater mInflator;

    private Context mContext = null;

    private int mCurrentPlayAnimPosition = -1;

    public AudioShoutingAdapter(Context mContext) {
        this.mContext = mContext;
        this.mInflator = LayoutInflater.from(mContext);
    }

    private AudioShoutingAdapter() {

    }

    /**
     * 或取文件列表
     */
    protected abstract List<File> getFileList();

    /**
     * 文件处理
     *
     * @param file file
     */
    protected abstract void toLongClickAction(File file);


    @Override
    public int getCount() {
        return getFileList().size();
    }


    @Override
    public Object getItem(int position) {
        return getFileList().get(position);
    }


    @Override
    public long getItemId(int position) {
        return position;
    }


    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final File file = getFileList().get(position);

        if (file == null || !file.exists()) {
            return convertView;
        }

        ViewHolder holder = null;

        if (convertView == null) {
            convertView = mInflator.inflate(R.layout.item_audio, null);

            holder = new ViewHolder();

            holder.voiceImageView = (VoiceImageView) convertView.findViewById(R.id.iv_voice);
            holder.pathTextView = (TextView) convertView.findViewById(R.id.file_path_textview);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.pathTextView.setText(file.getName());

        holder.voiceImageView.setOnClickListener(v -> {

            final VoiceImageView mView = (VoiceImageView) v;


            Uri audioUri = Uri.fromFile(file);

            AudioPlayManager.getInstance().startPlay(mContext, audioUri, new IAudioPlayListener() {
                @Override
                public void onStart(Uri var1) {
                    mView.startPlay();
                }

                @Override
                public void onStop(Uri var1) {
                    mView.stopPlay();
                }

                @Override
                public void onComplete(Uri var1) {
                    mView.stopPlay();
                }
            });
        });


        convertView.setOnLongClickListener(v -> {
            ToastUtils.showShort("删除成功");
            toLongClickAction(file);
            return false;
        });

        return convertView;
    }

    static class ViewHolder {
        VoiceImageView voiceImageView;
        TextView pathTextView;
    }
}
